package priv.bruce.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import priv.bruce.entry.MovieInfo;
import priv.bruce.service.MovieInfoService;

public class FindMovieInfoByNameServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private MovieInfoService movieInfoService = new MovieInfoService();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		String movieName = request.getParameter("movie_name");
		movieName = new String(movieName.getBytes("iso-8859-1"), "utf-8");
		try {
			List<MovieInfo> findByName = movieInfoService.findByName(movieName);
			response.getWriter().append(JSON.toJSONString(findByName));
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		String movieName = request.getParameter("movie_name");
		try {
			List<MovieInfo> findByName = movieInfoService.findByName(movieName);
			response.getWriter().append(JSON.toJSONString(findByName));
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
